package com.example.restatr.Atcivity.CitySubway;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.restatr.Atcivity.CitySubway.Bean.MetroLineBean;
import com.example.restatr.Atcivity.CitySubway.Fragment.MetroLineFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MetroLineAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ImageView img;
    private TabLayout Tab;
    public static ViewPager vp;
    public static List<MetroLineBean.DataBean> dataBeans = new ArrayList<>();
    private List<Fragment> fragments=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_metro_line_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        img = findViewById(R.id.img);
        Tab = findViewById(R.id.Tab);
        vp = findViewById(R.id.vp);
        Tools.SetBarTitle(toolbar, barTitle, "地铁线路", search, this,false);

        GetImg();
        GetLine();

    }
    //获取城市铁总览图
    private void GetImg() {
        new Thread(() -> {
            try {
                String get = Tools.Get("/prod-api/api/metro/city", "");
                JSONObject jsonObject = new JSONObject(get);
                if (jsonObject.getInt("code") == 200) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String imgUrl = data.getString("imgUrl");
                    runOnUiThread(() -> {
                        Glide.with(this)
                                .load(Tools.ip + imgUrl)
                                .into(img);
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }).start();
    }
    //查询城市所有线路
    private void GetLine() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/metro/line/list", "");
            MetroLineBean metroLineBean = new Gson().fromJson(get, MetroLineBean.class);
            dataBeans.addAll(metroLineBean.getData());
            runOnUiThread(() -> {
                if (dataBeans.size() > 0) {
                    for (int i = 0; i < dataBeans.size(); i++) {
                        fragments.add(new MetroLineFragment());
                    }
                    vp.setOffscreenPageLimit(fragments.size());
                    Tab.setupWithViewPager(vp);
                    if (getSupportFragmentManager()!=null){
                        vp.setAdapter(new MetroLineFragmentPagerAdapter(getSupportFragmentManager()));
                    }
                }
            });
        }).start();
    }




    public class MetroLineFragmentPagerAdapter extends FragmentPagerAdapter {
        public MetroLineFragmentPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return dataBeans.get(position).getLineName();
        }
    }



}